<?php
namespace App\Services;

use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Log;
use App\Lib\ossSdk\src\OSS\OssClient;
use App\Lib\ossSdk\src\OSS\Core\OssException;

class OssService{

    private $enable = TRUE;
    private $accessKeyId = 'LTAIeYQxrsxDpjZ8';
	private	$accessKeySecret = '13KIcjbJDPtzUMHcoDELtCIgFkmqCr';
	private	$endpoint = 'oss-me-east-1.aliyuncs.com';
    private	$bucket = 'fun-pro-media';
    
    //上传对象
    public function ossUpload($ossPath,$tempPath)
    {
        $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);

        $res = $ossClient->uploadFile($this->bucket,$ossPath,$tempPath);
        // dump($res);
        if($res['info']['http_code']==200){
            return json_encode(['code'=>1,'msg'=>'success']);
        }else{
            return json_encode(['code'=>0,'msg'=>'error']);
        }
    }

    //删除对象
    public function ossDelete($ossPath){
        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = $this->endpoint;
        // 存储空间名称
        $bucket= $this->bucket;
    
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        $res = $ossClient->deleteObject($bucket, $ossPath);
        // dump($res);
        if($res['info']['http_code'] == 204){
            return json_encode(['code'=>1,'msg'=>'success']);
        }else{
            return json_encode(['code'=>0,'msg'=>'error']);
        }
    }
}